package com.lanyanhua.job;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.util.Date;

@SpringBootApplication
@RestController
public class JobServerApplication {

	public static void main(String[] args) {
		SpringApplication.run(JobServerApplication.class, args);
	}

	/**
	 * 获取图片并显示在页面
	 */
	@RequestMapping(value = "/getImage/{fileName}")
	public void queryPic(@PathVariable("fileName") String fileName, HttpServletResponse response) throws Exception {

		if (fileName != null) {
			response.setContentType("image/jpeg");
			FileInputStream is = getFileStream(fileName);
			if (is != null) {
				int i = is.available(); // 得到文件大小
				byte[] data = new byte[i];
				is.read(data);// 读数据
				is.close();
				response.setContentType("image/jpeg");  // 设置返回的文件类型
				OutputStream toClient = response.getOutputStream(); // 得到向客户端输出二进制数据的对象
				toClient.write(data); // 输出数据
				toClient.close();
			}
		}
	}

	public FileInputStream getFileStream(String fileName) throws Exception {
		File filePic = new File("E:\\var\\images\\images\\" + fileName);
		return new FileInputStream(filePic);
	}
	@RequestMapping("/sendEmail")
	public void contextLoads() {
		try {
			//@Autowired
			JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();

		    /* javaMailSender.setHost("smtp.qq.com");  //设置邮件服务器
            javaMailSender.setUsername("3092575337");
            javaMailSender.setPassword("lciqtgdxytakdggb");
            javaMailSender.setPort(465);*/
        /*	javaMailSender.setHost("172.20.190.110");  //设置邮件服务器
			javaMailSender.setUsername("oaadmin");
			javaMailSender.setPassword("!QAZ2wsx#EDC");
            javaMailSender.setPort(25);*/
			javaMailSender.setHost("172.20.190.110");  //设置邮件服务器
			javaMailSender.setUsername("oaadmin");
			javaMailSender.setPassword("!QAZ2wsx#EDC");


			javaMailSender.setPort(25);

			//建立邮件消息
			MimeMessage msg = javaMailSender.createMimeMessage();
			MimeMessageHelper msgHelper = new MimeMessageHelper(msg, true, "utf-8");
			 	//发送者
			msgHelper.setFrom("oaadmin@luolai.com.cn");
			//msgHelper.setFrom("3092575337@qq.com");

			//msg.setRecipient(Message.RecipientType.CC, new InternetAddress("3092575337@qq.com"));
			//msgHelper.setFrom("oaadmin@luolai.com.cn");
			//msgHelper.setFrom("3092575337@qq.com");
			//抄送
			//msg.setRecipient(Message.RecipientType.CC, new InternetAddress("2209434823@qq.com"));
			// 设置多个收件人地址

			//接收者
			msgHelper.setTo("lanyanhua@luolai.com.cn");
			//发送的标题
			msgHelper.setSubject("你好，熊迪");
			//发送的内容
			msgHelper.setText("亚 ~ 蛇 ~ 啦 ~ 雷");
			javaMailSender.send(msg);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@RequestMapping("/testJobA")
	public Result testJobA(){
		System.out.println("开始执行定时任务-A  \t"+new Date());
		return new Result(true,"200","定时任务-A，执行成功");
	}

	@RequestMapping("/testJobB")
	public Result testJobB(){
		System.out.println("开始执行定时任务-B  \t"+new Date());
		return new Result(true,"200","定时任务-B，执行成功");
	}
	private int i = 0;
	@RequestMapping("/testJobC")
	public Result testJobC() throws Exception {
		System.out.println("开始执行定时任务-C  \t"+new Date());
		if(i++ %2 == 0){
			System.out.println("-------手动抛出异常----C");
			throw new Exception("出现异常！！！");
		}
		return new Result(true,"200  \t","定时任务-C，执行成功");
	}
	@RequestMapping("/testJobD")
	public Result testJobD() throws InterruptedException {
		System.out.println("开始执行定时任务-D"+new Date());
		Thread.sleep( 1000*60*20	);
		System.out.println("开始执行结束"+new Date());
		return new Result(true,"200","定时任务-D，执行成功");
	}
		@RequestMapping("/serverCheck")
	public String testServerCheck() throws Exception {
		if(i++ >2){
			System.out.println("-------手动抛出异常----serverCheck");
			throw new Exception("出现异常！！！");
		}
		return "成功！";
	}

	@Bean
	public JavaMailSender getJavaMailSender(){
		return new JavaMailSenderImpl();
	}


}
